<?php
include ('../mysql.php');
$msg='';
header("Content-Type:text/vnd.wap.wml");
print '<?xml version="1.0" encoding="utf8"?>';
print '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml1.2.dtd">';
print '<wml>';
print '<head><meta http-equiv="Cache-Control" content="no-cache" forua="true"/></head>';
print '<card id="index" title="MRAK">';

    $query = "SELECT
      `id`
    FROM
      quest.game
    WHERE
      `start_date` <= NOW() AND `status`=2
    ORDER BY
      `start_date`
    LIMIT
      1;";
$sql = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($sql)==1){
	$sql=mysql_fetch_assoc($sql);
	$query = "UPDATE
      quest.game
      SET
      `status`=3
        WHERE `id`={$sql['id']};";
	mysql_query($query) or die(mysql_error());
 	//echo "НАЧАЛИ! (Перевели игру в состояние 3 процессе)";    
    
}


//Проверяем, идет ли уже игра.
$query = "SELECT
      `id`,`title`
    FROM
      quest.game
    WHERE
      `status`=3
    ORDER BY
      `start_date`
    LIMIT
      1;";
$sql = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($sql)==1){
    $sql=mysql_fetch_assoc($sql);
    $game_id=$sql['id']; //Гэйм Айди, на всю игру
    $game_title=$sql['title'];
    require('game.php');
}
else
{
//check_game_start();


    $query = "SELECT
      `id`, `title`
    FROM
      quest.game
    WHERE
      `start_date` >= NOW() AND (`status`=2 OR `status`=1)
    ORDER BY
      `start_date`
    LIMIT
      1;";
	$sql = mysql_query($query) or die(mysql_error());
    	if (mysql_num_rows($sql)==1){
		$sqlgame = mysql_fetch_assoc($sql);
       	$msg .= "Ближайшая игра ".$sqlgame['title'];
		}
		else {
    	$msg .= "Нет запланированных игр.";
		}
}


print '</card>
</wml>';

?>
